package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 李城
 * @version 1.0
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询护理项目
     * @param name     护理项目名称
     * @param status   状态：0-禁用，1-启用
     * @param pageNum  当前页码
     * @param pageSize 每页显示数量
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = nursingProjectMapper.selectByPage(name, status);
        PageResponse<NursingProjectVo> pageResponse = new PageResponse(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.insertNursingPorject(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        try {
            nursingProjectMapper.updateNursingProject(BeanUtil.toBean(nursingProjectDto, NursingProject.class));
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NP_INSERT_FAIL);
        }
    }

    @Override
    public void deleteNursingProject(Long id) {
        nursingProjectMapper.deleteNursingProject(id);
    }


    @Override
    public void updateNursingProjectStatus(Long id, Integer status) {
        nursingProjectMapper.updateNursingProjectStatus(id, status);
    }

    @Override
    public List<NursingProjectVo> getAllNursingProject() {
        return nursingProjectMapper.getAllNursingProject();
    }

}
